package com.iwomy.secureplat.platform.controller;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iwomy.secureplat.common.core.util.R;
import com.iwomy.secureplat.common.log.annotation.SysLog;
import com.iwomy.secureplat.common.security.util.SecurityUtils;
import com.iwomy.secureplat.platform.dto.BasicDeviceAttributeDataDTO;
import com.iwomy.secureplat.platform.entity.BasicDeviceAttributeDataEntity;
import com.iwomy.secureplat.platform.service.BasicDeviceAttributeDataService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.iwomy.secureplat.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 设备属性值和设备关系表
 *
 * @author pig
 * @date 2023-11-27 18:29:01
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/basicDeviceAttributeData" )
@Tag(description = "basicDeviceAttributeData" , name = "设备属性值和设备关系表管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class BasicDeviceAttributeDataController {

    private final  BasicDeviceAttributeDataService basicDeviceAttributeDataService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param basicDeviceAttributeDataDTO 设备属性值和设备关系表
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('platform_basicDeviceAttributeData_view')" )
    public R getBasicDeviceAttributeDataPage(@ParameterObject Page page, @ParameterObject BasicDeviceAttributeDataDTO basicDeviceAttributeDataDTO) {
        return R.ok(basicDeviceAttributeDataService.getBasicDeviceAttributeDataPage(page, basicDeviceAttributeDataDTO));
    }


    /**
     * 通过id查询设备属性值和设备关系表
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('platform_basicDeviceAttributeData_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(basicDeviceAttributeDataService.getById(id));
    }

    /**
     * 新增设备属性值和设备关系表
     * @param basicDeviceAttributeData 设备属性值和设备关系表
     * @return R
     */
    @Operation(summary = "新增设备属性值和设备关系表" , description = "新增设备属性值和设备关系表" )
    @SysLog("新增设备属性值和设备关系表" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('platform_basicDeviceAttributeData_add')" )
    public R save(@RequestBody BasicDeviceAttributeDataEntity basicDeviceAttributeData) {
		basicDeviceAttributeData.setDeptId(SecurityUtils.getUser().getDeptId());
        return R.ok(basicDeviceAttributeDataService.save(basicDeviceAttributeData));
    }

    /**
     * 修改设备属性值和设备关系表
     * @param basicDeviceAttributeData 设备属性值和设备关系表
     * @return R
     */
    @Operation(summary = "修改设备属性值和设备关系表" , description = "修改设备属性值和设备关系表" )
    @SysLog("修改设备属性值和设备关系表" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('platform_basicDeviceAttributeData_edit')" )
    public R updateById(@RequestBody BasicDeviceAttributeDataEntity basicDeviceAttributeData) {
        return R.ok(basicDeviceAttributeDataService.updateById(basicDeviceAttributeData));
    }

    /**
     * 通过id删除设备属性值和设备关系表
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除设备属性值和设备关系表" , description = "通过id删除设备属性值和设备关系表" )
    @SysLog("通过id删除设备属性值和设备关系表" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('platform_basicDeviceAttributeData_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(basicDeviceAttributeDataService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param basicDeviceAttributeData 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('platform_basicDeviceAttributeData_export')" )
    public List<BasicDeviceAttributeDataEntity> export(BasicDeviceAttributeDataEntity basicDeviceAttributeData,Long[] ids) {
        return basicDeviceAttributeDataService.list(Wrappers.lambdaQuery(basicDeviceAttributeData).in(ArrayUtil.isNotEmpty(ids), BasicDeviceAttributeDataEntity::getId, ids));
    }
}